var doing = false;
var path;
var cur;
var shopId;


$(document).ready(function(){
	path = $("#path").val();
	shopId = $("#shopId").val();
	
	//cart delete item
	$(".J_deleteBtn").live("click",deleteItem);
	
	//cart update number
	$(".J_subtract").live("click",amountDown);
	//$(".J_num").live("blur",amountInput);
	$(".J_add").live("click",amountUp);
	
	// refresh
//	$(".refresh").live("click",reloadCart);
	
	// find shoppers
	//$("#settlement-btn").live("click",settlement);
	
	// hide shopper box
	//$(".popBg").live("click",hideShopperBox);
	
	// 扫一扫
	//$(".scan-btn").live("click",scanMerchandise);
	
});

function reloadCart(){
	var cartUrl = path+"/cart/index/"+shopId+" #cart-list";
	$("#cart-box").load(cartUrl,function(responseText, textStatus, jqXHR){
		if(textStatus == "success" || textStatus == "notmodified"){
	        loaded();
	    }
	});
}

function scanMerchandise(){
	wx.scanQRCode({
		needResult : 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
		scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
		success : function(res) {
			if(isEmpty(res.resultStr)){
				return;
			}
			if(res.resultStr.indexOf("/shopper/connectShopper/") != -1){
				$.ajax({
				    type: 'POST',
				    url: res.resultStr ,
				    dataType: "text",
				    success:function(result) {
				    	if(result.indexOf("success") != -1){
				    		result ="success";
				    		$.dialog({
								content : "向导购求助成功",
								title : '温馨提示',
								time:2000,
								lock : true
							});
				    	}else{
				    		$.dialog({
								content : "扫码失败",
								title : '温馨提示',
								time:2000,
								lock : true
							});
				    	}
				    	
				    }
				});
			}else{
				var resultCode = res.resultStr.split(",")[1];
				if(isEmpty(resultCode)){
					return;
				}
				$.ajax({
				    type: 'POST',
				    url:path+"/merchandise/findByBarcode/"+resultCode,
				    dataType: "text",
				    success:function(result) {
				    	if("" != result && null !=result){
				    		window.location.href=path+"/merchandise/"+result+".html";
				    	}else{
					    	$.dialog({
								content : "商品不存在",
								title : '温馨提示',
								time:2000,
								lock : true
							});
					    	
				    	}
				    }
				});
			}
		}
	});
}

/*function addCart(resultCode){
	var url = path + "/cart/add/"+shopId+"/"+resultCode;
	$.ajax({
	    type: 'POST',
	    url: url ,
	    dataType: "text",
	    success:function(result) {
	    	var content = "";
        	switch (result) {
			case 'YYK-10000':
				var cartUrl = path+"/cart/index/"+shopId+" #cart-list";
				$("#cart-box").load(cartUrl,function(responseText, textStatus, jqXHR){
					if(textStatus == "success" || textStatus == "notmodified"){
				        loaded();
				    }
				});
				break;
			case 'YYK-20002':
				content = "每箱不能超过3公斤";
				break;
			case 'YYK-20003':
				content = "每箱数量不能超过10";
				break;
			case 'YYK-20004':
				content = "每箱不能超过120欧元";
				break;
			default:
				content = "系统繁忙，请稍候重试";
				break;
			}
        	// 显示消息
        	if(content != ""){
        		$.dialog({
    				content : content,
    				title : '温馨提示',
    				time:2000,
    				lock : true
    			});
        	}
	    	
	    } ,error:function(){
	    	$.dialog({
				content : '系统繁忙，请稍候重试',
				title : '温馨提示',
				time:2000,
				lock : true
			});
	    }
	});
}*/

function hideShopperBox(){
	$(".popDiv").hide();
}

function setShoppers(shoppers){
	
	var shoppersTags = "";
	for (var i = 0; i < shoppers.length; i++) {
		var shopper = shoppers[i];
		
		var shopperTags = '<li id='+shopper.shopperId+' class="shopper"><div class="salePic fl">';
		shopperTags += '<img src="'+path+shopper.shopperPhoto+'"></div>';
		shopperTags += '<div class="saleName fr">'+shopper.shopperLastName+ shopper.shopperFirstName;
		shopperTags +='</div></li>';
		
		shoppersTags += shopperTags;
	}
	
	$(".popDiv").find("ul").html(shoppersTags);
	$(".popDiv").show(); // 显示导购列表
}

/**
 * 删除购物车商品
 */
function deleteItem(){
	cur = $(this);
	if(doing){
		return;
	}
	doing = true;
	
	$.dialog({
		content : "确定删除吗？",
		title : '温馨提示',
		ok : function() {
			doDeleteItem();
		},
		cancel : function() {
			doing = false;
		},
		lock : true
	});
}


function doDeleteItem(){
	
	var curData = cur.parents(".spec").attr("id").split("_");
	
	var url = path+"/cart/delete/"+shopId+"-"+curData[2];
	
	$.post(url, function(result){
		if(result == "YYK-10000"){
			doing = false;
			var cartUrl = path+"/cart/index/"+shopId+" #cart-list";
			$("#cart-box").load(cartUrl,function(responseText, textStatus, jqXHR){
				if(textStatus == "success" || textStatus == "notmodified"){
			        loaded();
			    }
			});
			return;
		}
		
		$.dialog({
			content : "删除失败，请重试",
			title : '温馨提示',
			time:2000,
			lock : true
		});
		doing = false;
	});
}

/**
 * 结算
 */
function settlement(){
	
	if(isEmpty(shopId)){
		return;
	}
	
	window.location.href=path+"/cart/settlement/"+shopId;
	
}

/*********************update number start**************************/
// 减少数量
function amountDown(){
	if(doing) return;
    doing = true;
	cur = $(this);
	
	var datas = cur.parents(".spec").attr("id").split("_");
	var merchandiseBarcode = datas[2];
	var number = datas[1];
	number--;
	
	updateNumber(merchandiseBarcode, number);
}

/*// 输入数量
function amountInput(){
	if(doing) return;
	
	cur = $(this);
	var datas = cur.parents(".spec").attr("id").split("_");
	var merchandiseBarcode = datas[2];
	var number = datas[1];
	
	if(isNaN(number) || number<=0 || !(/^\d+$/.test(number))){
		
		$.dialog({
			content : "只能输入非0的正整数！",
			title : '鑫网提示',
			ok : function() {
			},
			lock : true
		});
		
		cur.val(data[5]);
		return;
	}
	
	if(number == data[5]){
		return;
	}
	doing = true;
	
	updateNumber(cur, number);
}*/

// 增加数量
function amountUp(){
	if(doing) return;
    doing = true;
	
	var cur = $(this);
	
	var datas = cur.parents(".spec").attr("id").split("_");
	var merchandiseBarcode = datas[2];
	var number = datas[1];
	number++;
	
	updateNumber(merchandiseBarcode, number);
}

function updateNumber(merchandiseBarcode,number){
	
	if(Number(number) <= 0){
		doing = false;
		return;
	}
	
	var url = path+"/cart/update/"+shopId+"-"+merchandiseBarcode+"-"+number;
	$.ajax({
        type: "post",
        dataType: "text",
        async:true,
        url:url,
        success: function (result) {
        	var content = "";
        	switch (result) {
			case 'YYK-10000':
				var cartUrl = path+"/cart/index/"+shopId+" #cart-list";
				$("#cart-box").load(cartUrl,function(responseText, textStatus, jqXHR){
					if(textStatus == "success" || textStatus == "notmodified"){
				        loaded();
				    }
				});
				break;
			case 'YYK-20002':
				content = "箱子已超重";
				break;
			case 'YYK-20003':
				content = "每箱不能超过10个商品";
				break;
			case 'YYK-20004':
				content = "每箱不能超过120欧元";
				break;

			default:
				content = "系统繁忙，请稍候重试";
				break;
			}
        	// 显示消息
        	if(content != ""){
        		$.dialog({
    				content : content,
    				title : '温馨提示',
    				time:2000,
    				lock : true
    			});
        	}
        	
			doing = false;
		},error: function(){
			$.dialog({
				content : "系统繁忙，请稍候重试",
				title : '温馨提示',
				time:2000,
				lock : true
			});
			doing = false;
		}
	 });
}
/*********************update number end**************************/
